using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using KFGame.Modes;

namespace KFGame.Editor
{


    public class KFGFEditWindow : EditorWindow
    {
        private string editname;
        private KFMode mode;
        private SerializedObject targer;
        private SerializedProperty itemlist;
        public string filepath;

        Vector2 scrollpos;
        public static void showWindow(string path)
        {
            var window = GetWindow<KFGFEditWindow>();
            window.titleContent = new GUIContent("KFGF Editor");
            window.setEditName(path);
            window.Show();

        }
        public void setEditName(string path)
        {
            editname = path;
            if ("KFConfig" == editname)
            {
                KFConfig();
            }
            // itemlist =new ReorderableList(serializedObject,serializedObject.FindProperty,true,true,true,true);
            if ("KeyBoardSimulation" == editname)
            {
                KeyBoardSimulation();
                return;
            }
            if ("SeralPortConfig" == editname)
            {
                SeralPortConfig();
                return;
            }
            if ("SeralPortEventConfig" == editname)
            {
                SeralPortEventConfig();
                return;
            }

            if ("SPEtoKFE" == editname)
            {
                SPEtoKFE();
                return;
            }
        }

        void OnDestroy()
        {
            mode.destroy();

        }

        void OnEnable()
        {
            logo = (Texture2D)Resources.Load("images/logo2", typeof(Texture2D));
        }

        Texture2D logo;
        private void drawLogo()
        {
            GUILayout.Label(logo);


        }



        /// <summary>
        ///  界面
        /// </summary>
        private void OnGUI()
        {
            EditorGUILayout.LabelField("File", filepath);

            if (GUILayout.Button("Save"))
            {
                mode.save();
                ShowNotification(new GUIContent("保存成功"));
            }

            if ("KFConfig" == editname)
            {
                OnGUI_KFConfig();
                return;
            }
            scrollpos = EditorGUILayout.BeginScrollView(scrollpos, GUILayout.Width(position.width));
            EditorGUILayout.PropertyField(itemlist, true, null);
            // targer.Update();
            // if(reorderableList!=null)
            // reorderableList.DoLayoutList();
            // targer.ApplyModifiedProperties();
            drawLogo();
            EditorGUILayout.EndScrollView();


        }

        private void OnGUI_KFConfig()
        {

        }
        private void KFConfig()
        {
            mode = KFConfigMode.getModes();
            mode.LoadDatas();

            filepath = mode.FileName();
        }

        private void SPEtoKFE()
        {
            mode = KFSPTOEConfigMode.getModes();
            mode.LoadDatas();
            targer = new SerializedObject(mode);
            itemlist = targer.FindProperty("eventModes");
            filepath = mode.FileName();
        }

        ReorderableList reorderableList;
        private void OnGUI_SeralPortEvent()
        {
            reorderableList = new ReorderableList(targer, itemlist, true, true, true, true);

            //设置单个元素的高度
            reorderableList.elementHeight = 80;

            //绘制单个元素
            reorderableList.drawElementCallback =
                (rect, index, isActive, isFocused) =>
                {
                    var element = itemlist.GetArrayElementAtIndex(index);
                      rect.height -= 4;
                    rect.y += 2;
                  var ds=   element.FindPropertyRelative("des"); 
                   EditorGUI.PropertyField(rect, ds);
                    var bf=   element.FindPropertyRelative("enter"); 
                   EditorGUI.PropertyField(rect, bf);
                    
                };
                  //背景色
        reorderableList.drawElementBackgroundCallback = (rect, index, isActive, isFocused) => {
            GUI.backgroundColor = Color.yellow;
        };

        //头部
        reorderableList.drawHeaderCallback = (rect) =>
            EditorGUI.LabelField(rect, itemlist.displayName);
        }
        private void SeralPortEventConfig()
        {
            mode = KFSeralPortEventConfigMode.getModes();
            mode.LoadDatas();
            targer = new SerializedObject(mode);
            itemlist = targer.FindProperty("eventModes");
            filepath = mode.FileName();
            OnGUI_SeralPortEvent();
        }

        private void SeralPortConfig()
        {
            mode = KFSeralPortConfigMode.getModes();
            mode.LoadDatas();
            targer = new SerializedObject(mode);
            itemlist = targer.FindProperty("eventModes");
            filepath = mode.FileName();
        }


        private void OnGUI_KeyBoardSimulation()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        private void KeyBoardSimulation()
        {
            mode = KFKeyEventConfigMode.getModes();
            mode.LoadDatas();
            targer = new SerializedObject(mode);
            itemlist = targer.FindProperty("eventModes");
            filepath = mode.FileName();
        }

    }
}